package com.cskaoyan.controller;


import com.cskaoyan.bean.data.SearchIndexData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.KeyWordService;
import com.cskaoyan.service.SearchHistoryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/search")
public class WxSearchController {
    @Autowired
    private KeyWordService keyWordService;
    @Autowired
    private SearchHistoryService searchHistoryService;

    @GetMapping("index")
    public BaseRespVo index() {
        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();

            SearchIndexData data = keyWordService.queryindex(userId);
            return BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }
    }


    @GetMapping("helper")
    public BaseRespVo helper(String keyword) {
        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();

            List<String> data = searchHistoryService.querySearchHistoryBykeyword(keyword, userId);
            return BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @PostMapping("clearhistory")
    public BaseRespVo clearhistory() {
        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();

            searchHistoryService.deleteSearchHistory(userId);
            return BaseRespVo.ok(null);
        } else {
            return BaseRespVo.unAuthc();
        }
    }
}
